package com.wxyzz.cem.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.wxyzz.cem.enums.ThreadLocalName;
import com.wxyzz.cem.exception.UserServiceException;
import com.wxyzz.cem.interceptor.BackAdminInterceptor;
import com.wxyzz.cem.interceptor.UserInterceptor;
import com.wxyzz.cem.pojo.User;
import com.wxyzz.cem.pojo.dto.UserDTO;
import com.wxyzz.cem.pojo.enums.UserRole;
import com.wxyzz.cem.service.UserService;
import com.wxyzz.cem.util.EncryptUtil;
import com.wxyzz.cem.util.Result;
import com.wxyzz.cem.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author xc
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;




    @UserInterceptor.UserIntercept
    @GetMapping("/getCurrentUser")
    public Result<User> getCurrentUser() {
        User user = userService.getCurrentUser();
        return Result.success(user);
    }

    @GetMapping("/page")
    public Result<PageDTO<User>> list(@RequestParam(required = false,defaultValue = "1") Integer page,
                                   @RequestParam(required = false,defaultValue = "10") Integer size,
                                   @RequestParam(required = false) String name,
                                   @RequestParam(required = false) String account,
                                   @RequestParam(required = false) String phone) {
        PageDTO<User> pageDTO = userService.PageList(page, size, name, account, phone);
        return Result.success(pageDTO);
    }
    
    @PostMapping
    public Result<String> register(@RequestBody UserDTO userDTO) {
        User user = new User();
        BeanUtil.copyProperties(userDTO, user);
        userService.register(user);
        return Result.success();
    }
    
    @PostMapping("login")
    public Result<String> login(@RequestBody UserDTO userDTO) {
        User user = new User();
        BeanUtil.copyProperties(userDTO, user);
        String token = userService.login(user);
        return Result.success(token);
    }

    @PutMapping("/update")
    @UserInterceptor.UserIntercept
    public Result<String> updateUser(@RequestBody UserDTO userDTO) {
        User userFromDB = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        User userFromParams = new User();
        BeanUtil.copyProperties(userDTO, userFromParams);
        
        userFromParams.setId(userFromDB.getId());
        userFromParams.setAccount(userFromDB.getAccount());
        
        String password = userFromParams.getPassword();
        if (StrUtil.isNotBlank(password)) {
            String encryptPassword = userService.encryptPassword(userFromParams);
            userFromParams.setPassword(encryptPassword);
        } else {
            userFromParams.setPassword(userFromDB.getPassword());
        }
        
        String newToken = userService.updateUser(userFromParams);
        return Result.success(newToken);
    }

    @PutMapping ("/disable/{id}")
    @BackAdminInterceptor.BackAdminIntercept
    public void disableUser(@PathVariable Integer id) {
        userService.disableUser(id);
    }

    @PutMapping("/enable/{id}")
    @BackAdminInterceptor.BackAdminIntercept
    public void enableUser(@PathVariable Integer id) {
        userService.enableUser(id);
    }
    
    @GetMapping("/token/refresh")
    @UserInterceptor.UserIntercept
    public Result<String> refreshToken() {
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        String token = userService.refreshTokenById(user.getId());
        return Result.success(token);
    }
    
    @PutMapping("/class-admin/{userId}")
    @UserInterceptor.UserIntercept
    public Result<Object> setClassAdmin(@PathVariable Integer userId) {
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        if (user.getRole() != UserRole.CLASS_ADMIN) {
            throw new UserServiceException("无权限");
        }
        userService.setClassAdmin(userId, user.getClassId());
        return Result.success();
    }
}
